package com.wzxy.parse.wx;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wzxy.common.Const;
import com.wzxy.spider.wx.entity.Topic;
import com.wzxy.spider.wx.service.impl.TopicService;
import com.wzxy.util.MyTimeUtils;
import com.wzxy.wzinfo.entiry.NewsIntroduce;
import com.wzxy.wzinfo.service.impl.NewsIntService;

/**
 * 解析原文
 * @author h
 *
 */
@Service
public class WxOriginArticle{

	@Autowired
	private NewsIntService newsIntService;	
	@Autowired
	private TopicService topicService;
	
	public void startParse(Topic toptic){
		
		if(StringUtils.isNotBlank(toptic.getHtml())){
				
			Document doc = Jsoup.parse(toptic.getHtml());
			NewsIntroduce introduce = new NewsIntroduce();
			
			String title = toptic.getTitle();	
			if(StringUtils.isBlank(title)){
				title = doc.select("[id=activity-name]").text();
			}
			introduce.setTitle(title);
			System.out.println("文章的标题：" + title);
	
			String postTime = doc.select("[id=post-date]").text();
			introduce.setPostTime(postTime);
			System.out.println("发布日期：" + postTime);
	
			String site = doc.select("[id=post-user]").text();
			introduce.setSource(Const.WX + site);
			System.out.println("来源网站：" + Const.WX + site);
			
			String content = doc.select("[id=js_content]").text();
			introduce.setContent(content);
			System.out.println("内容：" + content);
			
			String parseTime = MyTimeUtils.getNowDateString();
			introduce.setCreationTime(parseTime);
			System.out.println("解析时间：" + parseTime);
			
			String url = toptic.getUrl();
			introduce.setURL(url);
			System.out.println("原文链接：" + url);
			
			newsIntService.save(introduce);
			toptic.setStatus(com.wzxy.common.Const.DONE);
			topicService.update(toptic, toptic.getId());
		}		
	}
	
	
}
